﻿using QuanLyThongTinCoSo.BaseForm;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;
using Model = Database.Models;

namespace QuanLyThongTinCoSo.Forms.Phong
{
    public partial class frmPhongDetail : frmDetail
    {
        private int phongId;

        private void LoadForm()
        {
            InitializeComponent();
            txtTenPhong.Focus();
            txtSo.Text = Constant.SoDetail.TenSo;
        }

        public frmPhongDetail()
        {
            LoadForm();
            Text = @"Thêm mới phòng";
        }

        public frmPhongDetail(int id)
        {
            LoadForm();
            Text = @"Sửa thông tin phòng";

            _fromtype = Constant.TypeForm.Edit;
            phongId = id;

            var phongBiz = new PhongBiz();
            var model = phongBiz.GetItemById(id) as Model.Phong;
            if (model == null) return;
            txtTenPhong.Text = model.TenPhong;
            txtDienGiai.Text = model.DienGiai;
        }

        #region Validation

        protected override bool Validation()
        {
            bool flag;

            errorProvider.Dispose();
            if (string.IsNullOrEmpty(txtTenPhong.Text))
            {
                errorProvider.SetError(txtTenPhong, "Bạn chưa nhập tên phòng. Xin vui lòng kiểm tra lại");
                flag = false;
            }
            else
            {
                flag = true;
            }

            return flag;
        }

        #endregion

        #region Insert

        protected override void Insert()
        {
            var phong = new Model.Phong();
            phong.Ma = txtMaPhong.Text.Trim();
            phong.TenPhong = txtTenPhong.Text.Trim();
            phong.DienGiai = txtDienGiai.Text.Trim();

            var phongBiz = new PhongBiz();
            phongBiz.Insert(phong);
        }

        #endregion

        protected override void Edit()
        {
            var phongBiz = new PhongBiz();
            var model = (Model.Phong) phongBiz.GetItemById(phongId);

            model.TenPhong = txtTenPhong.Text;
            model.DienGiai = txtDienGiai.Text.Trim();

            phongBiz.Update(model);
        }
    }
}